package tienmanh.gs.goldminer.manager;

import java.util.ArrayList;
import java.util.HashMap;

import org.andengine.engine.Engine;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.ui.IGameInterface;
import org.andengine.ui.IGameInterface.OnCreateSceneCallback;

import tienmanh.gs.goldminer.base.BaseScene;
import tienmanh.gs.goldminer.scene.GameScene;
import tienmanh.gs.goldminer.scene.FlashScene;
import tienmanh.gs.goldminer.scene.GameScene;
import tienmanh.gs.goldminer.scene.LoadScene;
import tienmanh.gs.goldminer.scene.MainMenuScene;
import tienmanh.gs.goldminer.scene.ShopScene;
//import tienmanh.gs.goldminer.scene.GameScene;
import tienmanh.gs.goldminer.scene.MapScene;
import tienmanh.gs.goldminer.type.SceneType;

public class SceneManager
{
	//---------------------------------------------
	// SCENES
	//---------------------------------------------
	
	private BaseScene splashScene;
	private BaseScene menuScene;
	private BaseScene gameScene;
	private BaseScene shopScene;
	private BaseScene mapScene;
	private BaseScene loadScene;
	
	//---------------------------------------------
	// VARIABLES
	//---------------------------------------------
	
	private static final SceneManager INSTANCE = new SceneManager();
	
	private SceneType currentSceneType = SceneType.SCENE_SPLASH;
	
	private BaseScene currentScene;
	
	private Engine engine = ResourcesManager.getInstance().engine;
	
	public enum SceneType
	{
		SCENE_SPLASH,
		SCENE_MENU,
		SCENE_GAME,
		SCENE_SHOP,
		SCENE_MAP,
		SCENE_LOAD
	}
	
	//---------------------------------------------
	// CLASS LOGIC
	//---------------------------------------------
	
	public void setScene(BaseScene scene)
	{
		engine.setScene(scene);
		currentScene = scene;
		currentSceneType = scene.getSceneType();
	}
	
	public void setScene(SceneType sceneType)
	{
		switch (sceneType)
		{
			case SCENE_MENU:
				setScene(menuScene);
				break;
			case SCENE_GAME:
				setScene(gameScene);
				break;
			case SCENE_SPLASH:
				setScene(splashScene);
				break;
			case SCENE_SHOP:
				setScene(shopScene);
				break;
			case SCENE_MAP:
				setScene(mapScene);
				break;
			case SCENE_LOAD:
				setScene(loadScene);
				break;
			default:
				break;
		}
	}
	
	public void createMenuScene()
	{
		menuScene = new MainMenuScene();
        SceneManager.getInstance().setScene(menuScene);
        disposeSplashScene();
	}
	
	public void createLoadScene( Engine mEngine)
	{
		loadScene = new LoadScene();
        SceneManager.getInstance().setScene(loadScene);
        
	}
	
	public void createSplashScene(OnCreateSceneCallback pOnCreateSceneCallback)
	{
		ResourcesManager.getInstance().loadSplashScreen();
		splashScene = new FlashScene();
		currentScene = splashScene;
		pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
	}
	
	private void disposeSplashScene()
	{
		ResourcesManager.getInstance().unloadSplashScreen();
		if(splashScene!= null)
		splashScene.disposeScene();
		splashScene = null;
	}
	
	public void loadShopScene(final  Engine mEngine)
	{
		if(mapScene != null)
			mapScene.disposeScene();
		shopScene = new ShopScene();
		setScene(shopScene);
	}
	
//	public void loadHelpScene(final  Engine mEngine)
//	{
//		helpScene = new HelpScene();
//		setScene(helpScene);
//	}
	
	public void loadMapScene(final  Engine mEngine)
	{
		if(gameScene != null)
			gameScene.disposeScene();
		mapScene = new MapScene();
		setScene(mapScene);
	}
	
	public void loadGameScene(final Engine mEngine)
	{
        		gameScene = new GameScene();
        		setScene(gameScene);
		
	}
	
	public void loadMenuScene(final Engine mEngine)
	{
		engine.start();
		if(mapScene != null)
			mapScene.disposeScene();
		if(gameScene != null)
		gameScene.disposeScene();
		ResourcesManager.getInstance().unloadGameTextures();
		createMenuScene();
	}
	
	//---------------------------------------------
	// GETTERS AND SETTERS
	//---------------------------------------------
	
	public static SceneManager getInstance()
	{
		return INSTANCE;
	}
	
	public SceneType getCurrentSceneType()
	{
		return currentSceneType;
	}
	
	public BaseScene getCurrentScene()
	{
		return currentScene;
	}
}